//
//  MKVisitorView.swift
//  101weibo
//
//  Created by 劢克科技 on 2019/1/18.
//  Copyright © 2019 劢克科技. All rights reserved.
//

import UIKit
import SnapKit

class MKVisitorView: UIView {
    
    lazy private var circleImage = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    lazy private var opaImage = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    lazy private var homeImage = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy private var msgLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.lightGray
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
    }()
    
    lazy private var btnBgImage: UIImage = {
        guard let image: UIImage = UIImage(named: "common_button_white_disable") else {
            return UIImage()
        }
        let imageW = image.size.width
        let imageH = image.size.height
        let resiableImage = image.stretchableImage(withLeftCapWidth: Int(imageW / 2), topCapHeight: Int(imageH / 2))
        return resiableImage
    }()
    
    lazy var registBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setBackgroundImage(btnBgImage, for: .normal)
        btn.setTitle("注册", for: .normal)
        btn.setTitleColor(UIColor.orange, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        return btn
    }()
    
    lazy var loginBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setBackgroundImage(btnBgImage, for: .normal)
        btn.setTitle("登录", for: .normal)
        btn.setTitleColor(UIColor.orange, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension MKVisitorView {
    
    func addRotationAnim() {
        let anim = CABasicAnimation(keyPath: "transform.rotation.z")
        
        anim.fromValue = 0
        anim.toValue = Double.pi * 2
        anim.repeatCount = MAXFLOAT
        anim.duration = 5
        anim.isRemovedOnCompletion = false
        
        circleImage.layer.add(anim, forKey: nil)
    }
    
    func customUI(iconName: String, title: String) {
        circleImage.isHidden = true
        homeImage.image = UIImage(named: iconName)
        msgLabel.text = title
    }
    
    private func setupUI() {
        backgroundColor = #colorLiteral(red: 0.9293106794, green: 0.929469943, blue: 0.9293007255, alpha: 1)
        
        addSubview(circleImage)
        addSubview(opaImage)
        addSubview(homeImage)
        addSubview(msgLabel)
        addSubview(registBtn)
        addSubview(loginBtn)
        
        circleImage.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        
        opaImage.snp.makeConstraints { (make) in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        
        homeImage.snp.makeConstraints { (make) in
            make.centerX.equalTo(circleImage)
            make.centerY.equalTo(self).offset(-60)
        }
        
        msgLabel.snp.makeConstraints { (make) in
            make.width.equalTo(220)
            make.centerX.equalTo(self)
            make.top.equalTo(circleImage.snp.bottom).offset(10)
        }
        
        registBtn.snp.makeConstraints { (make) in
            make.left.equalTo(msgLabel.snp.left).offset(20)
            make.top.equalTo(msgLabel.snp.bottom).offset(10)
            make.width.equalTo(70)
            make.height.equalTo(32)
        }
        
        loginBtn.snp.makeConstraints { (make) in
            make.right.equalTo(msgLabel.snp.right).offset(-20)
            make.top.equalTo(msgLabel.snp.bottom).offset(10)
            make.width.equalTo(70)
            make.height.equalTo(32)
        }
    }
}
